<?php
class Wee_Logging_Model_Handler_Controllers{
     public function postDispatchGeneric($config, $eventModel, $processorModel){
         if ($collectedIds = $processorModel -> getCollectedIds()){
             $eventModel -> setInfo(Mage :: helper('wee_logging') -> implodeValues($collectedIds));
             return true;
             }
         return false;
         }
     public function postDispatchSimpleSave($config, $eventModel){
         return true;
         }
     public function postDispatchConfigView($config, $eventModel){
         $id = Mage :: app() -> getRequest() -> getParam('section');
         if (!$id){
             $id = 'general';
             }
         $eventModel -> setInfo($id);
         return true;
         }
     public function postDispatchConfigSave($config, $eventModel, $processor){
         $request = Mage :: app() -> getRequest();
         $postData = $request -> getPost();
         $groupFieldsData = array();
         $change = Mage :: getModel('wee_logging/event_changes'); //Collect skip encrypted fields 
         $encryptedNodeEntriesPaths = Mage :: getSingleton('adminhtml/config') -> getEncryptedNodeEntriesPaths(true);
        $skipEncrypted = array();
        foreach ($encryptedNodeEntriesPaths as $fieldName){
            $skipEncrypted[] = $fieldName['field'];
        } //For each group of current section creating separated event change 
         if (isset($postData['groups'])){
            foreach ($postData['groups'] as $groupName => $groupData){
                foreach ($groupData['fields'] as $fieldName => $fieldValueData){ // Clearing config data accordingly to collected skip fields
                     if (!in_array($fieldName, $skipEncrypted) && isset($fieldValueData['value'])){
                        $groupFieldsData[$fieldName] = $fieldValueData['value'];
                    }
                }
                $processor -> addEventChanges(clone $change -> setSourceName($groupName) -> setOriginalData(array()) -> setResultData($groupFieldsData));
                $groupFieldsData = array();
            }
        }
        $id = $request -> getParam('section');
        if (!$id){
            $id = 'general';
        }
        return $eventModel -> setInfo($id);
    }
    public function postDispatchCategoryMove($config, $eventModel){
        return $eventModel -> setInfo(Mage :: app() -> getRequest() -> getParam('id'));
    }
    public function postDispatchGlobalSearch($config, $eventModel){
        return $eventModel -> setInfo(Mage :: app() -> getRequest() -> getParam('query'));
    }
    public function postDispatchForgotPassword($config, $eventModel){
        if (Mage :: app() -> getRequest() -> isPost()){
            if ($model = Mage :: registry('wee_logging_saved_model_adminhtml_index_forgotpassword')){
                $info = $model -> getId();
            }else{
                $info = Mage :: app() -> getRequest() -> getParam('email');
            }
            $success = true;
            $messages = Mage :: getSingleton('adminhtml/session') -> getMessages() -> getLastAddedMessage();
            if ($messages){
                $success = 'error' != $messages -> getType();
            }
            return $eventModel -> setIsSuccess($success) -> setInfo($info);
        }
        return false;
    }
    public function postDispatchPollValidation($config, $eventModel){
        $out = json_decode(Mage :: app() -> getResponse() -> getBody());
        if (!empty($out -> error)){
            $id = Mage :: app() -> getRequest() -> getParam('id');
            return $eventModel -> setIsSuccess(false) -> setInfo($id == 0 ? '' : $id);
        }else{
            $poll = Mage :: registry('current_poll_model');
            if ($poll && $poll -> getId()){
                return $eventModel -> setIsSuccess(true) -> setInfo($poll -> getId());
            }
        }
        return false;
    }
    public function postDispatchCustomerValidate($config, $eventModel){
        $out = json_decode(Mage :: app() -> getResponse() -> getBody());
        if (!empty($out -> error)){
            $id = Mage :: app() -> getRequest() -> getParam('id');
            return $eventModel -> setIsSuccess(false) -> setInfo($id == 0 ? '' : $id);
        }
        return false;
    }
    public function postDispatchReport($config, $eventModel, $processor){
        $fullActionNameParts = explode('_report_', $config -> getName(), 2);
        if (empty($fullActionNameParts[1])){
            return false;
        }
        $request = Mage :: app() -> getRequest();
        $filter = $request -> getParam('filter');
        $data = array_intersect_key($request -> getParams(), array('report_from' => null, 'report_to' => null, 'report_period' => null, 'store' => null, 'website' => null, 'group' => null));
        if ($filter){
            $filterData = Mage :: app() -> getHelper('adminhtml') -> prepareFilterString($filter);
            $data = array_merge($data, (array)$filterData);
        }
        if ($data){
            $change = Mage :: getModel('wee_logging/event_changes');
            $processor -> addEventChanges($change -> setSourceName('params') -> setOriginalData(array()) -> setResultData($data));
        }
        return $eventModel -> setInfo($fullActionNameParts[1]);
    }
    public function postDispatchPromoCatalogApply($config, $eventModel){
        $request = Mage :: app() -> getRequest();
        return $eventModel -> setInfo($request -> getParam('rule_id') ? $request -> getParam('rule_id') : 'all rules');
    }
    public function postDispatchPromoCatalogSaveAndApply($config, $eventModel, $processorModel){
        $request = Mage :: app() -> getRequest();
        $this -> postDispatchGeneric($config, $eventModel, $processorModel);
        if ($request -> getParam('auto_apply')){
            $eventModel -> setInfo(Mage :: helper('wee_logging') -> __('%s & applied', $eventModel -> getInfo()));
        }
        return $eventModel;
    }
    public function postDispatchMyAccountSave($config, $eventModel){
    }
    public function postDispatchNewsletterUnsubscribe($config, $eventModel){
        $id = Mage :: app() -> getRequest() -> getParam('subscriber');
        if (is_array($id)){
            $id = implode(', ', $id);
        }
        return $eventModel -> setInfo($id);
    }
    public function postDispatchTaxRatesImport($config, $eventModel){
        if (!Mage :: app() -> getRequest() -> isPost()){
            return false;
        }
        $success = true;
        $messages = Mage :: getSingleton('adminhtml/session') -> getMessages() -> getLastAddedMessage();
        if ($messages){
            $success = 'error' != $messages -> getType();
        }
        return $eventModel -> setIsSuccess($success) -> setInfo(Mage :: helper('wee_logging') -> __('Tax Rates Import'));
    }
    public function postDispatchSystemStoreSave($config, $eventModel){
    }
    public function postDispatchProductUpdateAttributes($config, $eventModel, $processor){
        $request = Mage :: app() -> getRequest();
        $change = Mage :: getModel('wee_logging/event_changes');
        $products = $request -> getParam('product');
        if (!$products){
            $products = Mage :: helper('adminhtml/catalog_product_edit_action_attribute') -> getProductIds();
        }
        if ($products){
            $processor -> addEventChanges(clone $change -> setSourceName('product') -> setOriginalData(array()) -> setResultData(array('ids' => implode(', ', $products))));
        }
        $processor -> addEventChanges(clone $change -> setSourceName('inventory') -> setOriginalData(array()) -> setResultData($request -> getParam('inventory', array())));
        $attributes = $request -> getParam('attributes', array());
        $status = $request -> getParam('status', null);
        if (!$attributes && $status){
            $attributes['status'] = $status;
        }
        $processor -> addEventChanges(clone $change -> setSourceName('attributes') -> setOriginalData(array()) -> setResultData($attributes));
        $websiteIds = $request -> getParam('remove_website', array());
        if ($websiteIds){
            $processor -> addEventChanges(clone $change -> setSourceName('remove_website_ids') -> setOriginalData(array()) -> setResultData(array('ids' => implode(', ', $websiteIds))));
        }
        $websiteIds = $request -> getParam('add_website', array());
        if ($websiteIds){
            $processor -> addEventChanges(clone $change -> setSourceName('add_website_ids') -> setOriginalData(array()) -> setResultData(array('ids' => implode(', ', $websiteIds))));
        }
        return $eventModel -> setInfo(Mage :: helper('wee_logging') -> __('Attributes Updated'));
    }
    public function postDispatchTaxClassSave($config, $eventModel){
        if (!Mage :: app() -> getRequest() -> isPost()){
            return false;
        }
        $classType = Mage :: app() -> getRequest() -> getParam('class_type');
        $classId = Mage :: app() -> getRequest() -> getParam('class_id');
        if ($classType == 'PRODUCT'){
            $eventModel -> setEventCode('tax_product_tax_classes');
        }
        $success = true;
        $messages = Mage :: getSingleton('adminhtml/session') -> getMessages() -> getLastAddedMessage();
        if ($messages){
            $success = 'error' != $messages -> getType();
        }
        return $eventModel -> setIsSuccess($success) -> setInfo($classType . ($classId ? ': #' . Mage :: app() -> getRequest() -> getParam('class_id') : ''));
    }
    public function postDispatchCustomerSegmentMatch($config, $eventModel){
        return '';
    }
    public function postDispatchSystemBackupsCreate($config, $eventModel){
        if ($backup = Mage :: registry('backup_model')){
            $eventModel -> setIsSuccess($backup -> exists()) -> setInfo($backup -> getFileName());
        }else{
            $eventModel -> setIsSuccess(false);
        }
        return $eventModel;
    }
    public function postDispatchSystemBackupsDelete($config, $eventModel){
        if ($backup = Mage :: registry('backup_model')){
            $eventModel -> setIsSuccess(!$backup -> exists()) -> setInfo($backup -> getFileName());
        }else{
            $eventModel -> setIsSuccess(false);
        }
        return $eventModel;
    }
    public function postDispatchAdminAccountsMassUnlock($config, $eventModel){
        if (!Mage :: app() -> getRequest() -> isPost()){
            return false;
        }
        $userIds = Mage :: app() -> getRequest() -> getPost('unlock', array());
        if (!is_array($userIds)){
            $userIds = array();
        }
        if (!$userIds){
            return false;
        }
        return $eventModel -> setInfo(implode(', ', $userIds));
    }
    public function postDispatchReindexProcess($config, $eventModel){
        $processIds = Mage :: app() -> getRequest() -> getParam('process', null);
        if (!$processIds){
            return false;
        }
        return $eventModel -> setInfo(is_array($processIds) ? implode(', ', $processIds) : (int)$processIds);
    }
    public function postDispatchSystemImportExportRun($config, $eventModel){
        $profile = Mage :: registry('current_convert_profile');
        if (!$profile){
            return false;
        }
        $success = true;
        $messages = Mage :: getSingleton('adminhtml/session') -> getMessages() -> getLastAddedMessage();
        if ($messages){
            $success = 'error' != $messages -> getType();
        }
        return $eventModel -> setIsSuccess($success) -> setInfo($profile -> getName() . ': #' . $profile -> getId());
    }
    public function postDispatchSystemCurrencySave($config, $eventModel, $processor){
        $request = Mage :: app() -> getRequest();
        $change = Mage :: getModel('wee_logging/event_changes');
        $data = $request -> getParam('rate');
        $values = array();
        if(!is_array($data)){
            return false;
        }
        foreach ($data as $currencyCode => $rate){
            foreach($rate as $currencyTo => $value){
                $value = abs($value);
                if($value == 0){
                    continue;
                }
                $values[] = $currencyCode . '=>' . $currencyTo . ': ' . $value;
            }
        }
        $processor -> addEventChanges($change -> setSourceName('rates') -> setOriginalData(array()) -> setResultData(array('rates' => implode(', ', $values))));
        $success = true;
        $messages = Mage :: getSingleton('adminhtml/session') -> getMessages() -> getLastAddedMessage();
        if ($messages){
            $success = 'error' != $messages -> getType();
        }
        return $eventModel -> setIsSuccess($success) -> setInfo(Mage :: helper('wee_logging') -> __('Currency Rates Saved'));
    }
    public function postDispatchSaveCacheSettings($config, $eventModel, $processor){
        $request = Mage :: app() -> getRequest();
        if (!$request -> isPost()){
            return false;
        }
        $info = '-';
        $cacheTypes = $request -> getPost('types');
        if (is_array($cacheTypes) && !empty($cacheTypes)){
            $cacheTypes = implode(', ', $cacheTypes);
            $info = Mage :: helper('wee_logging') -> __('Cache types: %s ', $cacheTypes);
        }
        $success = true;
        $messages = Mage :: getSingleton('adminhtml/session') -> getMessages() -> getLastAddedMessage();
        if ($messages){
            $success = 'error' != $messages -> getType();
        }
        return $eventModel -> setIsSuccess($success) -> setInfo($info);
    }
    public function postDispatchTaxRatesExport($config, $eventModel){
        if (!Mage :: app() -> getRequest() -> isPost()){
            return false;
        }
        $success = true;
        $messages = Mage :: getSingleton('adminhtml/session') -> getMessages() -> getLastAddedMessage();
        if ($messages){
            $success = 'error' != $messages -> getType();
        }
        return $eventModel -> setIsSuccess($success) -> setInfo(Mage :: helper('wee_logging') -> __('Tax Rates Export'));
    }
    public function postDispatchSalesArchiveManagement($config, $eventModel){
        $request = Mage :: app() -> getRequest();
        $ids = $request -> getParam('order_id', $request -> getParam('order_ids'));
        if (is_array($ids)){
            $ids = implode(', ', $ids);
        }
        return $eventModel -> setInfo($ids);
    }
}
 